//城市道路临时占用

function selectDlzyQyxx(tplcode) {
  ExtElWin({id: 'selectQyxxWin', title: '选择申请单位', w: 550, h: 400, tplcode: tplcode});
}

function selectDlzySgdw(tplcode){
	ExtElWin({id: 'selectSgdwWin', title: '选择临时占用单位', w: 550, h: 400, tplcode: tplcode});
}

function ExtElWin(f) {
  var w = 350;
  var h = 160;
  if (f.w) w = f.w;
  if (f.h) h = f.h;
  var qyxxGrid = new Ext.ux.grid.YwGrid({
    region: 'center',
    layout: 'fit',
    tplcode: f.tplcode,
    needDbclick:false,
    margins: '1 1 1 1',
    needSelect4CsdlwjTbar:true,
    url: '/yw-com-qyxx!dataList.action?templateCode='+f.tplcode,
    keyField: 'id',
    defaultSortField: 'sqrq',
    structure: [{
      name: 'id',
      hidden: true
    },{
      name: 'post',
      hidden: true
    },{
      name: 'qyjc',
      hidden: true
    },{
      name: 'qyphone',
      hidden: true
    },{
      name: 'cz',
      hidden: true
    },{
      name: 'frxm',
      hidden: true
    },{
      name: 'frphone',
      hidden: true
    },{
      name: 'frtel',
      hidden: true
    },{
      name: 'gszcdjh',
      hidden: true
    },{
      name: 'qydlyszh',
      hidden: true
    },{
      name: 'gszcdjyxq',
      hidden: true
    },{
      name: 'qydlyszyxq',
      hidden: true
    },{
      name: 'ywlxr',
      hidden: true
    },{
      name: 'ywlxphone',
      hidden: true
    },{
      name: 'ywlxrtel',
      hidden: true
    },{
      name: 'address',
      hidden: true
    },{
      name: 'email',
      hidden: true
    },{
      name: 'zczj',
      hidden: true
    },{
      name: 'csmj',
      hidden: true
    },{
      name: 'frzw',
      hidden: true
    },{
      name: 'zzdj',
      hidden: true
    },{
      name: 'zsbh',
      hidden: true
    },{
      name: 'jjlx',
      hidden: true
    },{
      name: 'qyRemarks',
      hidden: true
    },{
      name: 'rsJsry',
      hidden: true
    },{
      name: 'rsWxg',
      hidden: true
    },{
      name: 'rsTotal',
      hidden: true
    },{
      name: 'dwslJyrq',
      hidden: true
    },{
      name: 'dwslGyrq',
      hidden: true
    },{
      name: 'qylx',
      hidden: true
    },{
      name: 'qyfzr',
      hidden: true
    },{
      name: 'sqdw',
      header: '企业名称'
    },{
      name: 'sqrq',
      header: '申请日期',
      width: 17
    }]
  });
  
  var store = qyxxGrid.store;
  var toolbar = qyxxGrid.toolbar;
  store.on('beforeload', function(){
    store.baseParams = {
      sqdw: toolbar.search_sqdw.getValue()
    };
  }, this);
  
  new Ext.Window({
    id        : f.id,
    title     : f.title,
    width     : w,
    height    : h,
    iconCls   : 'icon_title',
    layout    : 'fit',
    modal     : true,
    plain     : true,
    resizeable: false,
    items     : [qyxxGrid]
  }).show();
}

function selectDlwjCallback(params) {
	try{
		if(document.forms[0].SR_DLZYSQB_SQDW && params.r.get('sqdw'))
			document.forms[0].SR_DLZYSQB_SQDW.value = params.r.get('sqdw');
		if(document.forms[0].SR_DLZYSQB_SQDW_ADDRESS && params.r.get('address'))
			document.forms[0].SR_DLZYSQB_SQDW_ADDRESS.value = params.r.get('address');
		if(document.forms[0].SR_DLZYSQB_FZR && params.r.get('fddbr'))
			document.forms[0].SR_DLZYSQB_FZR.value = params.r.get('fddbr');
		if(document.forms[0].SR_DLZYSQB_SQDW_TEL && params.r.get('ywlxphone'))
			document.forms[0].SR_DLZYSQB_SQDW_TEL.value = params.r.get('ywlxphone');
		if(document.forms[0].SR_DLZYSQB_LXR && params.r.get('ywlxr'))
			document.forms[0].SR_DLZYSQB_LXR.value = params.r.get('ywlxr');
		if(document.forms[0].SR_DLZYSQB_QYBH && params.r.get('id'))
			document.forms[0].SR_DLZYSQB_QYBH.value = params.r.get('id');
		if(document.forms[0].SR_DLZYSQB_SQDW_TEL && params.r.get('ywlxrtel'))
			document.forms[0].SR_DLZYSQB_SQDW_TEL.value = params.r.get('ywlxrtel');
		if(document.forms[0].SR_DLZYSQB_FZR && params.r.get('qyfzr'))
			document.forms[0].SR_DLZYSQB_FZR.value = params.r.get('qyfzr');
		if(document.forms[0].SR_DLZYSQB_FZH && params.r.get('zsbh'))
			document.forms[0].SR_DLZYSQB_FZH.value = params.r.get('zsbh');
			
		
	}catch(e){}
}

function selectDlwjJsdwCallback(params){
	try{
		/*
		if(document.forms[0].SR_DLZYSQB_ZYDW && params.r.get('sqdw'))
			document.forms[0].SR_DLZYSQB_ZYDW.value = params.r.get('sqdw');
		if(document.forms[0].SR_DLZYSQB_ZYDW_ADDRESS && params.r.get('address'))
			document.forms[0].SR_DLZYSQB_ZYDW_ADDRESS.value = params.r.get('address');
		if(document.forms[0].SR_DLZYSQB_AQFZR && params.r.get('fddbr'))
			document.forms[0].SR_DLZYSQB_AQFZR.value = params.r.get('fddbr');
		if(document.forms[0].SR_DLZYSQB_ZYDW_TEL && params.r.get('qyphone'))
			document.forms[0].SR_DLZYSQB_ZYDW_TEL.value = params.r.get('qyphone');
		if(document.forms[0].SR_DLZYSQB_ZYDW_LXR && params.r.get('ywlxr'))
			document.forms[0].SR_DLZYSQB_ZYDW_LXR.value = params.r.get('ywlxr');
			*/
		
	}catch(e){}
}
var preTab;
var curTab;

//给tabPanel添加事件，tab变换的时候，
//触发事件
function addEventListenerToTab(){
	var tabPanel = parent.Ext.getCmp('centerPanel');
	curTab = tabPanel;
	if(tabPanel){
		if(!tabPanel.hasListener('tabchange')){
			chooseToViewTab();
			tabPanel.addListener('tabchange',changeEvent);
		}
	}
}

/**
 * 根据是否是联合审批案件，选择显示哪些tab
 */
function chooseToViewTab(){
	var centerPanel__gzsrxdggcszyscb01_form = parent.document.getElementById('centerPanel__gzsrxdggcszyscb01_form');
	var centerPanel__dlzyyqb_form = parent.document.getElementById('centerPanel__dlzyyqb_form');
	var centerPanel__dlzybjgz_form = parent.document.getElementById('centerPanel__dlzybjgz_form');
	var centerPanel__srsfb_form = parent.document.getElementById('centerPanel__srsfb_form');
	var centerPanel__csdllszysp_cjcl_form = parent.document.getElementById('centerPanel__csdllszysp_cjcl_form');
	
	var centerPanel__zy_gcxmjbxx_form = parent.document.getElementById('centerPanel__zy_gcxmjbxx_form');
	var centerPanel__zy_qyjbxx_form = parent.document.getElementById('centerPanel__zy_qyjbxx_form');
	var centerPanel__zy_qyzzjbxx_form = parent.document.getElementById('centerPanel__zy_qyzzjbxx_form');
	var centerPanel__zy_sqcl_form = parent.document.getElementById('centerPanel__zy_sqcl_form');
	
	var mainPanel = parent.document.getElementById('gzsrxdggcszysqb_formIframe').contentWindow.document;
	var lhspId = mainPanel.getElementsByName('SR_DLZYSQB_LHSP_ID')[0].value;
	if("" != lhspId){//联合审批案件 隐藏panel
		if(centerPanel__dlzyyqb_form){
			centerPanel__dlzyyqb_form.style.display="none";//隐藏延期表
		}
		if(centerPanel__dlzybjgz_form){
			centerPanel__dlzybjgz_form.style.display="none";//隐藏补交告知表
		}
		if(centerPanel__srsfb_form){
			centerPanel__srsfb_form.style.display="none";//隐藏收费表
		}
		if(centerPanel__csdllszysp_cjcl_form){
			centerPanel__csdllszysp_cjcl_form.style.display="none";//隐藏收件材料表
		}
	}else{
		if(centerPanel__zy_gcxmjbxx_form){
			centerPanel__zy_gcxmjbxx_form.style.display="none";//隐藏工程基本信息表
		}
		if(centerPanel__zy_qyjbxx_form){
			centerPanel__zy_qyjbxx_form.style.display="none";//隐藏企业基本信息表
		}
		if(centerPanel__zy_qyzzjbxx_form){
			centerPanel__zy_qyzzjbxx_form.style.display="none";//隐藏企业资质基本信息表
		}
		if(centerPanel__zy_sqcl_form){
			centerPanel__zy_sqcl_form.style.display="none";//隐藏申请材料表
		}
	}
}

//重写setTimeout方法，以使能够传递参数
//IE下不能传递参数
function setTimeoutForIE(pFunction, iTime, params){
     var fuckIE = function(){
             pFunction(params);    
      }
      var pTimeout = setTimeout(fuckIE, iTime);
      return pTimeout;
}

//事件的响应
function changeEvent(obj){
	var item = obj.getActiveTab();
	if(item == null){
		return;
	}
	preTab = curTab;
	curTab = item;
	
	var id = item.id;
	if(id == 'gzsrxdggcszyscb01_form'){ //转向行政审查表
		var main = parent.document.getElementById('gzsrxdggcszysqb_formIframe').contentWindow.document;
		var field = main.getElementsByName('id')[0];
		if(main && field && field.value){
			item.setDisabled(true);
			setTimeoutForIE(copyElements2Sc,1800,item);
		}else{
			parent.Ext.getCmp('centerPanel').setActiveTab(0);
			item.setDisabled(true);
			Ext.Msg.alert('系统提示','请先保存基本信息表单，再进行行政审查表的查看！');
		}
	}else if(id == 'csdllszysp_cjcl_form'){//转向收件材料
		var main = parent.document.getElementById('gzsrxdggcszysqb_formIframe').contentWindow.document;
		var field = main.getElementsByName('id')[0];
		if(main && field && field.value != ''){
			var QYBH = main.getElementsByName('SR_DLZYSQB_QYBH')[0].value;
			var AjId = main.getElementsByName('id')[0].value;
			item.setDisabled(false);
			changeUrlAndReload(item,AjId,QYBH);
		}else{
			parent.Ext.getCmp('centerPanel').setActiveTab(0);
			item.setDisabled(true);
			Ext.Msg.alert('系统提示','请先保存基本信息表单，再进行收件材料的上传！');
		}
	}else if(id == 'Show_WEBGIS'){ //GIS页面
		var main = parent.document.getElementById('gzsrxdggcszysqb_formIframe').contentWindow.document;
		var objectId = main.getElementsByName('id')[0];
		if(main && objectId){
			item.setDisabled(false);
			setTimeout(copy2Gis,1000);
			
		}else{
			item.setDisabled(true);
		}
	}else if(id == 'gzsrxdggcszysqb_form'){  //转向申请表，拷贝审查表的数据
		copysfb2sqb();
		var distab = parent.Ext.getCmp('gzsrxdggcszyscb01_form');
		if((preTab.id == 'gzsrxdggcszyscb01_form') && distab.disabled == false)
			copyBz();
	}else if(id == 'srsfb_form'){//转向收费表
		copyElements2Sf(item);
	}else if(id == 'dlzy_gs_form'){//转向公示表
		//copyElements2Gs(item);
	}
}

//把申请表的信息copy到收费表
function copyElements2Sf(item){
		var doc = parent.document.getElementById('gzsrxdggcszysqb_formIframe').contentWindow.document;
		var sfb;
		if(parent.document.getElementById('srsfb_formIframe')){
			sfb = parent.document.getElementById('srsfb_formIframe').contentWindow.document;
		}
		if(sfb.readyState == "complete"){
			if(doc && sfb){
				sfb.getElementsByName('sfje')[0].value = doc.getElementsByName('SR_DLZYSQB_sfje')[0].value;
			 	sfb.getElementsByName('sfbh')[0].value = doc.getElementsByName('SR_DLZYSQB_sfbh')[0].value;
			 	sfb.getElementsByName('fph')[0].value = doc.getElementsByName('SR_DLZYSQB_fph')[0].value;		
			}
		}else{
		window.setInterval(function(){ 
			copyElements2Sf(item)
		}, 500); 
	}
}

function copysfb2sqb(){
	var doc = parent.document.getElementById('gzsrxdggcszysqb_formIframe').contentWindow.document;
	var sfb;
	if(parent.document.getElementById('srsfb_formIframe')){
		sfb = parent.document.getElementById('srsfb_formIframe').contentWindow.document;
	}
	if(doc && sfb){
		doc.getElementsByName('SR_DLZYSQB_sfje')[0].value = sfb.getElementsByName('sfje')[0].value;
		doc.getElementsByName('SR_DLZYSQB_sfbh')[0].value = sfb.getElementsByName('sfbh')[0].value;
		doc.getElementsByName('SR_DLZYSQB_fph')[0].value = sfb.getElementsByName('fph')[0].value;		
	}
}

function copyBz(){
	var doc =parent.document.getElementById('gzsrxdggcszyscb01_formIframe').contentWindow.document;
	var sqb =parent.document.getElementById('gzsrxdggcszysqb_formIframe').contentWindow.document;
	if(doc && sqb){
		var bzcontent = doc.getElementsByName('SR_DLZYSCB_BZ')[0];
		var bzhidden = doc.getElementsByName('SR_DLZYSQB_BZ')[0];
		if(bzcontent && bzhidden)
			bzhidden.value = bzcontent.value;
	}
}

function copy2Gis(){
	var main = parent.document.getElementById('gzsrxdggcszysqb_formIframe').contentWindow.document;
	var gis = parent.document.getElementById('Show_WEBGISIframe').contentWindow.document;
	var acceptNo = main.getElementsByName('SR_DLZYSQB_ACCEPT_NO')[0].value;
	var templateCode = WF_CONFIG.templateCode;
	var id = main.getElementsByName('id')[0].value;
	if(gis){
		//var ano = gis.getElementsByName('acceptNo')[0].value;
		//var ajid = gis.getElementsByName('id')[0].value;
		//var tid = gis.getElementsByName('templateCode')[0].value;
		//if(ano != acceptNo && ajid != id && tid != templateCode){
			//var src = ctx+'/gzcgwsp/csdlwj/flex.jsp?acceptNo='+acceptNo+'&templateCode='+templateCode+'&id='+id ;
			//parent.Ext.getDom("Show_WEBGISIframe").contentWindow.location.href = src;
		//}
	  	var src = ctx+'/gzcgwsp/csdlwj/flex.jsp?acceptNo='+acceptNo+'&templateCode='+templateCode+'&id='+id ;
		//parent.Ext.getDom("Show_WEBGISIframe").src = src;
		parent.Ext.getDom("Show_WEBGISIframe").contentWindow.location.href = src;
  	}
}

//改变收件材料的路径
function changeUrlAndReload(item,AjId,QyId){
	var isupdate="";//环节名称对文书的操作参数
    if(WF_CONFIG.activityName!="jbrcs" && WF_CONFIG.activityName!="cksa"){
    	isupdate="false";
    }
	if(!Ext.isEmpty(AjId)){
		item.setDisabled(false);
		if(item){
			var elementCode ='csdllszysp_cjcl_form';
			var src = 'yw-com-sjcllist!ywList.action?tplcode='+WF_CONFIG.templateCode+'&ajid='+AjId+'&qyid='+QyId+'&isUpdate='+isupdate;
			parent.Ext.getDom("csdllszysp_cjcl_formIframe").contentWindow.location.href = src;
		}
	}else{
		parent.Ext.getCmp('centerPanel').setActiveTab(0);
		item.setDisabled(true);
		Ext.Msg.alert('系统提示','请先保存基本信息表，后再查看收件材料表！');
	}
}

function copyElements2Sc(item){
	var sqb = parent.document.getElementById('gzsrxdggcszysqb_formIframe').contentWindow.document;
	var cpb = parent.document.getElementById('gzsrxdggcszyscb01_formIframe').contentWindow.document;
	if(cpb.readyState == "complete"){
		var id = sqb.getElementsByName('id')[0].value;
		if(id != null && id != ''){
			item.setDisabled(false);
			var aa = sqb.getElementsByName('SR_DLZYSQB_ACCEPT_NO');
			var bb = cpb.getElementsByName('SR_DLZYSCB_BH');
			if(sqb.getElementsByName('SR_DLZYSQB_ACCEPT_NO') && cpb.getElementsByName('SR_DLZYSCB_BH'))
				bb[0].value = aa[0].value;
			if(sqb.getElementsByName('SR_DLZYSQB_SQDW') && cpb.getElementsByName('SR_DLZYSCB_SQR'))
				 cpb.getElementsByName('SR_DLZYSCB_SQR')[0].value = sqb.getElementsByName('SR_DLZYSQB_SQDW')[0].value;
			if(cpb.getElementsByName('SR_DLZYSCB_XZXKMC'))
				cpb.getElementsByName('SR_DLZYSCB_XZXKMC')[0].value = '城市道路临时占用许可审查';
			if(sqb.getElementsByName('SR_DLZYSQB_BZ') && cpb.getElementsByName('SR_DLZYSCB_BZ'))
				cpb.getElementsByName('SR_DLZYSCB_BZ').value = sqb.getElementsByName('SR_DLZYSQB_BZ').value;
		}else{

//			parent.Ext.getCmp('centerPanel').setActiveTab(0);
//			item.setDisabled(true);
//			Ext.Msg.alert('系统提示','请先保存基本信息表，后再查看行政审查表！');
		}
	}else{
		window.setInterval(function(){ 
			copyElements2Sc(item)
		}, 500); 
	}
}

//把申请表的信息copy到公示表
function copyElements2Gs(item){
	var gsb = parent.document.getElementById('dlzy_gs_formIframe').contentWindow.document;
	var sqb = parent.document.getElementById('gzsrxdggcszysqb_formIframe').contentWindow.document;
	if(gsb.readyState == "complete"){
		var id = sqb.getElementsByName('id')[0].value;
		if(id != null && id != ''){
			item.setDisabled(false);
			if(sqb.getElementsByName('SR_DLZYSQB_SQDW') && gsb.getElementsByName('sqdw')){
				gsb.getElementsByName('sqdw')[0].value = sqb.getElementsByName('SR_DLZYSQB_SQDW')[0].value;
			}
			if(sqb.getElementsByName('SR_DLZYSQB_ZYDW') && gsb.getElementsByName('zydw')){
				gsb.getElementsByName('zydw')[0].value = sqb.getElementsByName('SR_DLZYSQB_ZYDW')[0].value;
			}
				
		}else{
			parent.Ext.getCmp('centerPanel').setActiveTab(0);
			item.setDisabled(true);
			Ext.Msg.alert('系统提示','请先保存基本信息表，后再查看公示表！');
		}
	}else{
		window.setInterval(function(){ 
			copyElements2Gs(item)
		}, 500); 
	}		
}

//表单的验证
function checkElementBeforeSubmit(){
	var obj;
	var sqb = parent.document.getElementById('gzsrxdggcszysqb_formIframe').contentWindow.document;
	var sqdw = sqb.getElementsByName('SR_DLZYSQB_SQDW')[0].value;
	var zydw = sqb.getElementsByName('SR_DLZYSQB_ZYDW')[0].value;
	var tbrq = sqb.getElementsByName('SR_DLZYSQB_TBRQ')[0].value;
	var sqdwtel = sqb.getElementsByName('SR_DLZYSQB_SQDW_TEL')[0].value;
	var lxr = sqb.getElementsByName('SR_DLZYSQB_LXR')[0].value;
	var zydwtel = sqb.getElementsByName('SR_DLZYSQB_ZYDW_TEL')[0].value;
	var zydwlxr = sqb.getElementsByName('SR_DLZYSQB_ZYDW_LXR')[0].value;
	if(sqdw == ''){
		obj = {
			result:false,
			text:'系统提示，申请单位不能为空！',
			target:sqb.getElementsByName('SR_DLZYSQB_SQDW')[0]
		};
		return obj;
	}
	if(sqdwtel == ''){
		obj = {
			result:false,
			text:'系统提示，电话不能为空！',
			target:sqb.getElementsByName('SR_DLZYSQB_SQDW_TEL')[0]
		};
		return obj;
	}
	if(lxr == ''){
		obj = {
			result:false,
			text:'系统提示，联系人不能为空！',
			target:sqb.getElementsByName('SR_DLZYSQB_LXR')[0]
		};
		return obj;
	}
	if(zydw == ''){
		obj = {
			result :false,
			text:'系统提示：临时占用单位不能为空！',
			target:sqb.getElementsByName('SR_DLZYSQB_ZYDW')[0]
		};
		return obj;
	}
	if(zydwtel == ''){
		obj = {
			result:false,
			text:'系统提示，电话不能为空！',
			target:sqb.getElementsByName('SR_DLZYSQB_ZYDW_TEL')[0]
		};
		return obj;
	}
	if(zydwlxr == ''){
		obj = {
			result:false,
			text:'系统提示，联系人不能为空！',
			target:sqb.getElementsByName('SR_DLZYSQB_ZYDW_LXR')[0]
		};
		return obj;
	}
	
	obj = {
		result:true,
		text:'',
		target:null
	};
	return obj;
}

//验证收件材料
function myValidate(id){
	var sjcl = parent.document.getElementById(id).contentWindow;
	var _sjclList = sjcl.document.getElementById('sjcl_list');
	var obj = null;
	if(_sjclList != null)
	{
		for(var i=1;i<_sjclList.rows.length;i++)
		{
			var _childNodes=_sjclList.rows[i].cells;
			if(_sjclList.rows[i].hasExists == 'false')
			{
			    var alertText =  '*'+_childNodes[1].innerHTML+'\u6CA1\u6709\u4E0A\u4F20,\u8BF7\u5148\u4E0A\u4F20\u8BE5\u9644\u4EF6!';
				obj = {text:alertText,result:false};
				return obj;
			}
		}
		obj = {text:null,result:true};
	}
    return obj;
}

function preViewXkzs(){
	var main = parent.document.getElementById('gzsrxdggcszysqb_formIframe').contentWindow.document;
	var id = main.getElementsByName('id')[0].value;
	var templateCode = WF_CONFIG.templateCode;
   	//if(<wf:hasPriv activities="jbrsj" />){
        openCenterWindow(ctx+'/dllszy!preViewXkzs.action?id='+id+'&hjmc=jbrsj&templateCode='+templateCode, 1100, 650);
    //}else if(<wf:hasPriv activities="dzgz" />){
    //   	openCenterWindow(ctx + '/dllszy!preViewXkzs.action?id='+id+'&hjmc=dzgz', 1100, 650);
    //}
    
}

function xkzgz(){
	var main = parent.document.getElementById('gzsrxdggcszysqb_formIframe').contentWindow.document;
	var id = main.getElementsByName('id')[0].value;
    openCenterWindow(ctx+'/dllszy!gzViewXkz.action?id='+id+'&hjmc=jbrbj', 1100, 650);
}

//公示
function gs(){
	var main = parent.document.getElementById('gzsrxdggcszysqb_formIframe').contentWindow.document;
	var id = main.getElementsByName('id')[0].value;
	var url="${ctx}/dllszy!gs.action";
	$.ajax({
		url : url,
		type : "post",
		dataType : "html",
		data : {
        'id': id
      },
		success : function(msg) {
			if(msg == "success"){
				alert("公示成功！");
			}else{
				alert(msg);
			}							
		},
		error:function() {
			alert("公示失败！");
		}
	});
}